package org.dromara.datacheck.controller.jd;

import cn.dev33.satoken.annotation.SaCheckPermission;
import cn.hutool.core.collection.CollUtil;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.constraints.NotEmpty;
import jakarta.validation.constraints.NotNull;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.dromara.common.core.domain.R;
import org.dromara.common.core.validate.AddGroup;
import org.dromara.common.core.validate.EditGroup;
import org.dromara.common.excel.core.ExcelResult;
import org.dromara.common.excel.utils.ExcelUtil;
import org.dromara.common.idempotent.annotation.RepeatSubmit;
import org.dromara.common.log.annotation.Log;
import org.dromara.common.log.enums.BusinessType;
import org.dromara.common.mybatis.core.page.PageQuery;
import org.dromara.common.mybatis.core.page.TableDataInfo;
import org.dromara.common.web.core.BaseController;
import org.dromara.datacheck.domain.bo.jd.JdSaleOutWarehouseOrderBo;
import org.dromara.datacheck.domain.jd.JdSaleOutWarehouseOrder;
import org.dromara.datacheck.domain.vo.jd.JdSaleOutWarehouseOrderImportVo;
import org.dromara.datacheck.domain.vo.jd.JdSaleOutWarehouseOrderVo;
import org.dromara.datacheck.listener.jd.JdSaleOutWarehouseOrderImportListener;
import org.dromara.datacheck.service.jd.IJdSaleOutWarehouseOrderService;
import org.springframework.http.MediaType;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.Arrays;
import java.util.List;

/**
 * 金蝶-销售出库单
 *
 * @author 刘武贵
 * @date 2024-09-08
 */
@Slf4j
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/dataCheck/jd/saleOutWarehouseOrder")
public class JdSaleOutWarehouseOrderController extends BaseController {

    private final IJdSaleOutWarehouseOrderService jdSaleOutWarehouseOrderService;

    /**
     * 查询金蝶-销售出库单列表
     */
    @SaCheckPermission("dataCheck:saleOutWarehouseOrder:list")
    @GetMapping("/list")
    public TableDataInfo<JdSaleOutWarehouseOrderVo> list(JdSaleOutWarehouseOrderBo bo, PageQuery pageQuery) {
        return jdSaleOutWarehouseOrderService.queryPageList(bo, pageQuery);
    }

    /**
     * 导出金蝶-销售出库单列表
     */
    @SaCheckPermission("dataCheck:saleOutWarehouseOrder:export")
    @Log(title = "金蝶-销售出库单", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(JdSaleOutWarehouseOrderBo bo, HttpServletResponse response) {
        List<JdSaleOutWarehouseOrderVo> list = jdSaleOutWarehouseOrderService.queryList(bo);
        ExcelUtil.exportExcel(list, "金蝶-销售出库单", JdSaleOutWarehouseOrderVo.class, response);
    }

    /**
     * 获取金蝶-销售出库单详细信息
     *
     * @param id 主键
     */
    @SaCheckPermission("dataCheck:saleOutWarehouseOrder:query")
    @GetMapping("/{id}")
    public R<JdSaleOutWarehouseOrderVo> getInfo(@NotNull(message = "主键不能为空")
                                     @PathVariable Long id) {
        return R.ok(jdSaleOutWarehouseOrderService.queryById(id));
    }

    /**
     * 新增金蝶-销售出库单
     */
    @SaCheckPermission("dataCheck:saleOutWarehouseOrder:add")
    @Log(title = "金蝶-销售出库单", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody JdSaleOutWarehouseOrderBo bo) {
        return toAjax(jdSaleOutWarehouseOrderService.insertByBo(bo));
    }

    /**
     * 修改金蝶-销售出库单
     */
    @SaCheckPermission("dataCheck:saleOutWarehouseOrder:edit")
    @Log(title = "金蝶-销售出库单", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody JdSaleOutWarehouseOrderBo bo) {
        return toAjax(jdSaleOutWarehouseOrderService.updateByBo(bo));
    }

    /**
     * 删除金蝶-销售出库单
     *
     * @param ids 主键串
     */
    @SaCheckPermission("dataCheck:saleOutWarehouseOrder:remove")
    @Log(title = "金蝶-销售出库单", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")
                          @PathVariable Long[] ids) {
        return toAjax(jdSaleOutWarehouseOrderService.deleteWithValidByIds(List.of(ids), true));
    }

    /**
     * 导入数据
     *
     * @param file          导入文件
     * @param updateSupport 是否更新已存在数据
     */
    @Log(title = "金蝶-销售出库单导入", businessType = BusinessType.IMPORT)
    @PostMapping(value = "/importData", consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
    public R<Void> importData(@RequestPart("file") MultipartFile file, boolean updateSupport) throws Exception {
        // 每次导入之前清空表里的数据
        List<JdSaleOutWarehouseOrder> jdOtherEnterWarhouseOrderList = jdSaleOutWarehouseOrderService.queryAll();
        if(CollUtil.isNotEmpty(jdOtherEnterWarhouseOrderList)){
            jdSaleOutWarehouseOrderService.deleteAll();
        }
        // 解析Excel文件并处理导入逻辑
        ExcelResult<JdSaleOutWarehouseOrderImportVo> result = ExcelUtil.importExcel(file.getInputStream(), JdSaleOutWarehouseOrderImportVo.class, new JdSaleOutWarehouseOrderImportListener(updateSupport));
        return R.ok(result.getAnalysis());
    }

    /**
     * 查询金蝶-销售出库单列表
     * 场景分类：寄售-寄寄售换货-发货(猫超售后店铺，进口猫超售后店铺)
     * 业务查询条件：根据sale_out_warehouse_order_customer客户为猫超寄售售后、进口超市售后店铺查询
     */
    @SaCheckPermission("dataCheck:saleOutWarehouseOrder:list")
    @GetMapping("/list-by-customer1")
    public TableDataInfo<JdSaleOutWarehouseOrderVo> listByCustomer1(JdSaleOutWarehouseOrderBo bo, PageQuery pageQuery) {
        bo.setSaleOutWarhouseOrderCustomerList(Arrays.asList("猫超寄售售后","进口超市售后店铺"));
        return jdSaleOutWarehouseOrderService.queryPageListByCustomer1(bo, pageQuery);
    }

    /**
     * 查询金蝶-销售出库单列表
     * 场景分类：分销（山姆）销售-销售
     * 业务查询条件：根据sale_out_warehouse_order_customer客户为山姆会员店查询
     */
    @SaCheckPermission("dataCheck:saleOutWarehouseOrder:list")
    @GetMapping("/list-by-customer2")
    public TableDataInfo<JdSaleOutWarehouseOrderVo> listByCustomer2(JdSaleOutWarehouseOrderBo bo, PageQuery pageQuery) {
        bo.setSaleOutWarhouseOrderCustomerList(Arrays.asList("山姆会员店"));
        return jdSaleOutWarehouseOrderService.queryPageListByCustomer2(bo, pageQuery);
    }

}
